﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace AGVCallSystem
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        System.Timers.Timer dataTimer = new System.Timers.Timer();
        DataSendReceive dataSendReceive = new DataSendReceive();
        SortedList<string, Label> LightList = new SortedList<string, Label>();
        CallDeviceBLL callDeviceBLL = new CallDeviceBLL();
        List<CallDeviceEntity> callDeviceList = new List<CallDeviceEntity>();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            dataSendReceive.openLightDelegate = OpenLightCallBack;
            dataSendReceive.closeLightDelegate = CloseLightCallBack;

            dataTimer.Interval = 500;
            dataTimer.Elapsed += SendDataTimer_Elapsed;
            dataTimer.AutoReset = true;

            callDeviceList = callDeviceBLL.GetCallByEnableState(true);
            int count = callDeviceList.Count;
            int column = (int)MapCanvas.Width / 60;
            int k = 0;
            if (count != 0)
            {
                for (int i = 0; i < count / column + 1; ++i)
                {
                    if (i == count / column)
                    {
                        for (int j = 0; j < count % column; ++j)
                        {
                            Label lb = new Label();
                            lb.Height = 35;
                            lb.Width = 45;
                            lb.Content = callDeviceList[k].CallDeviceNum;
                            lb.HorizontalContentAlignment = HorizontalAlignment.Center;
                            lb.VerticalContentAlignment = VerticalAlignment.Center;
                            lb.Background = Brushes.LightGray;
                            Canvas.SetLeft(lb, j * 60);
                            Canvas.SetTop(lb, i * 60);
                            MapCanvas.Children.Add(lb);
                            LightList.Add(callDeviceList[k].CallDeviceNum, lb);
                            k++;
                        }
                    }
                    else
                    {
                        for (int j = 0; j < column; ++j)
                        {
                            Label lb = new Label();
                            lb.Height = 35;
                            lb.Width = 45;
                            lb.Content = callDeviceList[k].CallDeviceNum;
                            lb.HorizontalContentAlignment = HorizontalAlignment.Center;
                            lb.VerticalContentAlignment = VerticalAlignment.Center;
                            lb.Background = Brushes.LightGray;
                            Canvas.SetLeft(lb, j * 60);
                            Canvas.SetTop(lb, i * 60);
                            MapCanvas.Children.Add(lb);
                            LightList.Add(callDeviceList[k].CallDeviceNum, lb);
                            k++;
                        }
                    }
                }
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            if (dataTimer.Enabled)
            {
                dataTimer.Close();
                dataTimer.Elapsed -= SendDataTimer_Elapsed;
            }
            dataSendReceive.ServerStop();
            Application.Current.Shutdown();
        }

        private void btn_start_Click(object sender, RoutedEventArgs e)
        {
            string msg = dataSendReceive.ServerStart();
            if (msg != "success")
            {
                MessageBox.Show(msg);
                return;
            }

            SetLightLabelInitState();

            if (!dataTimer.Enabled)
            {
                dataTimer.Start();
            }

            this.btn_start.IsEnabled = false;
            this.im_start.Source = new BitmapImage(new Uri("pack://application:,,,/Images/start_disable.png"));
            this.btn_pause.IsEnabled = true;
            this.im_pause.Source = new BitmapImage(new Uri("pack://application:,,,/Images/pause.png"));
            this.btn_setting.IsEnabled = false;
            this.im_setting.Source = new BitmapImage(new Uri("pack://application:,,,/Images/setting_disable.png"));
            this.btn_manage.IsEnabled = false;
            this.im_manage.Source = new BitmapImage(new Uri("pack://application:,,,/Images/manage_disable.png"));
            this.tb_status.Text = "启动";
        }

        private void btn_pause_Click(object sender, RoutedEventArgs e)
        {
            string msg = dataSendReceive.ServerStop();
            if (msg != "success")
            {
                MessageBox.Show(msg);
                return;
            }

            if (dataTimer.Enabled)
            {
                dataTimer.Stop();
            }

            this.btn_start.IsEnabled = true;
            this.im_start.Source = new BitmapImage(new Uri("pack://application:,,,/Images/start.png"));
            this.btn_pause.IsEnabled = false;
            this.im_pause.Source = new BitmapImage(new Uri("pack://application:,,,/Images/pause_disable.png"));
            this.btn_setting.IsEnabled = true;
            this.im_setting.Source = new BitmapImage(new Uri("pack://application:,,,/Images/setting.png"));
            this.btn_manage.IsEnabled = true;
            this.im_manage.Source = new BitmapImage(new Uri("pack://application:,,,/Images/manage.png"));
            this.tb_status.Text = "停止";
        }

        private void btn_setting_Click(object sender, RoutedEventArgs e)
        {
            SettingWindow setting = new SettingWindow();
            setting.ShowDialog();
        }

        private void btn_manage_Click(object sender, RoutedEventArgs e)
        {
            ComSettingWindow comSetting = new ComSettingWindow();
            comSetting.ShowDialog();
        }

        private void SendDataTimer_Elapsed(object sender, EventArgs e)
        {
            UpdateLightState();
        }

        private void OpenLightCallBack(string callNum)
        {
            LightList[callNum].Dispatcher.Invoke((Action)(() =>
            {
                LightList[callNum].Background = Brushes.Lime;
            }));
        }

        private void CloseLightCallBack(string callNum)
        {
            LightList[callNum].Dispatcher.Invoke((Action)(() =>
            {
                LightList[callNum].Background = Brushes.LightGray;
            }));
        }

        private void SetLightLabelInitState()
        {
            callDeviceList = callDeviceBLL.GetCallByEnableState(true);
            for (int i = 0; i < callDeviceList.Count; ++i)
            {
                if (callDeviceList[i].ExecuteState == true && callDeviceList[i].LightState == true)
                {
                    LightList[callDeviceList[i].CallDeviceNum].Background = Brushes.Yellow;
                }
                else if (callDeviceList[i].LightState == true)
                {
                    LightList[callDeviceList[i].CallDeviceNum].Background = Brushes.Lime;
                }
            }
        }

        private void UpdateLightState()
        {
            callDeviceList = callDeviceBLL.GetCallByEnableState(true);
            for (int i = 0; i < callDeviceList.Count; ++i)
            {
                if (callDeviceList[i].ExecuteState == true && callDeviceList[i].LightState == true)
                {
                    LightList[callDeviceList[i].CallDeviceNum].Dispatcher.Invoke((Action)(() =>
                    {
                        LightList[callDeviceList[i].CallDeviceNum].Background = Brushes.Yellow;
                    }));
                }

                if (callDeviceList[i].CallState == false && callDeviceList[i].LightState == true)
                {
                    dataSendReceive.SendCloseLightCommand(callDeviceList[i].CallDeviceNum);
                    Thread.Sleep(20);
                }
                else if (callDeviceList[i].CallState == true && callDeviceList[i].LightState == false)
                {
                    dataSendReceive.SendOpenLightCommand(callDeviceList[i].CallDeviceNum);
                    Thread.Sleep(20);
                }
            }
        }
    }
}
